﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adams.Public.Web
{
    public class MobileCheck
    {
    }

    public class MobileCheckAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            string strUserAgent = filterContext.HttpContext.Request.UserAgent.ToString().ToLower();
            string nomobile = filterContext.HttpContext.Request["nomobile"];

            if (string.IsNullOrEmpty(nomobile))
            {
                if (strUserAgent != null)
                {
                    if (filterContext.HttpContext.Request.Browser.IsMobileDevice == true ||
                        strUserAgent.Contains("iphone") ||
                        strUserAgent.Contains("blackberry") ||
                        strUserAgent.Contains("mobile") ||
                        strUserAgent.Contains("windows ce") ||
                        strUserAgent.Contains("opera mini") ||
                        strUserAgent.Contains("android") ||
                        strUserAgent.Contains("palm"))
                    {
                        string mobileurl = Shared.Config.GetAppSetting("MobileUrl");
                        string path = filterContext.HttpContext.Request.Path;
                        filterContext.HttpContext.Response.Redirect(mobileurl + path);
                    }
                    else
                    {
                        base.OnActionExecuting(filterContext);
                    }
                }
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }
    }
}